﻿// +--------------------------------------------------------------------+
// | codelibrary 														|
// +--------------------------------------------------------------------+
// | Copyright (c) 2009 AceNet	 										|
// +--------------------------------------------------------------------+
// | 说明：该库建立在prototype 1.5+库的基础上					   			|
// +--------------------------------------------------------------------+
// | Author: Wan Xianjia <okwxj@qq.com>								    |
// +--------------------------------------------------------------------+

/*
表格行滑过时效果类
参数table_id为使用该效果的表格
表格必须使用tbody
忽略该效果的行在该行上加class="noLight"
e.g:   new MoveOverLightTable("a_Table");
*/

function MoveOverLightTable(table_id){
	this.table_id = table_id;
	var self = this;
	
	this.regTable = function(){
		var trs = $$("#"+table_id+" tbody tr");
		
		if (trs.length == 0){
			return;
		}
		var bgcolor = trs[0].style.backgroundColor;
		for(var i = trs.length - 1; i >= 0; i--){
			if(trs[i].className.indexOf("noLight") == -1){
				trs[i].onmouseover = function(){
					this.style.backgroundColor = "#F6EEFC";
				}
				trs[i].onmouseout = function(){
					this.style.backgroundColor = bgcolor;
				}
			}
		}
	}
	Event.observe(window,'load',this.regTable);
}

/* 
确认删除类
new ConfirmDelForLink();
在需要确认的删除按钮或链接上添加class:delLink	
*/

function ConfirmDelForLink(){
	
	this.regConfirm = function(){
		var oArr = $$(".delLink");
		for(var i = oArr.length-1;i >= 0;i--){
			oArr[i].onclick = function(){
				if(confirm(this.title+"?")){
					return true;
				}else{
					return false;
				}
			}
		}
	}
	Event.observe(window,'load',this.regConfirm);
}

/* 
全选类
参数1：全选按钮的id
参数2：将被全部选中的className
页面加载完成时调用
e.g: new AllCheck('chk_btn','checks');
*/

var AllCheck = Class.create();

AllCheck.prototype = {
	initialize: function(btn_id,chk_class){
		$(btn_id).onclick = function(){
			var chks = $$("input."+chk_class);
			if($(btn_id).checked){
				for(var i = chks.length - 1; i >= 0; i--){
					chks[i].checked = true;
				}
			}else{
				for(var i = chks.length - 1; i >= 0; i--){
					chks[i].checked = false;
				}
			}
		}
	}
};

/*
表单检查类
new ValidateForm('loginForm',[checkEmail,checkEmpty]);
在页面加载完成时创建验证对象
参数1：要验证的Form ID
参数2：要调用的验证函数
参数3：
还需在需要验证的表单元素中添加相应的class(请查看验证函数)
如：<input class="isEmail" type="text">
如需要改变抛出信息方法，请修改throwErr函数。
*/

var ValidateForm = Class.create();

ValidateForm.prototype = {
	initialize:function(formId, funcArr, preFunArr) {
		this.idForm = formId;
		this.illegalArr = [];
		this.config = { //过滤规则表
			isNotEmpty : "_o.value == ''",
			isEmail: "_o.value != '' && !/^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/.test(_o.value)"
		};
		this.funcArr = funcArr;
		this.preFunArr = preFunArr ? preFunArr : '';
		//this.options = options? options : [];
		$(formId).onsubmit = this.validate.bind(this);
	},
	
	/* 表单提交前预处理函数 */
	
	preProcess: function(){
		if(this.preFunArr != ''){
			
			if(typeof this.preFunArr == 'function'){
				this.preFunArr();
			}
			
			if(this.preFunArr.constructor == Array){
				for(var i = this.preFunArr.length - 1; i >= 0; i --){
					this.preFunArr[i]();
				}	
			}
		}
	},
	
	/* 规则处理 */
	collect: function(str){
		if(str != ''){
			var arr = [];
			var o = $$('#'+this.idForm+' .'+str)
			
			for(var i = o.length - 1; i >= 0; i--){
				var rules = this.config[str].replace(/_o/g,'o[i]');
				if(eval('('+rules+')')){
					arr.push(o[i]);
				}
			}
		}
		return arr;
	},

	/* 收集未通过验证的表单元素和错误消息 */
	validate: function() {
		
		if(!this.funcArr || this.funcArr.length < 1) return;
		
		$$('#'+this.idForm+' input').each(function(node){
			node.value = node.value.replace(/(^\s*)|(\s*$)/g,"");	//清除前后空格
		});
		
		$$('#'+this.idForm+' textarea').each(function(node){
			node.value = node.value.replace(/(^\s*)|(\s*$)/g,"");									   
		});
		
		for(var i = 0; i < this.funcArr.length; i++){
			var result = this.collect(this.funcArr[i]);
			
			if(result){
				this.illegalArr = this.illegalArr.concat(result);
			}
		}

		for(var i = this.illegalArr.length - 1; i >= 0; i--){
			if(this.illegalArr[i].disabled || this.illegalArr[i].display == 'none'){
				this.illegalArr.splice(i,1);
			}
		}
		return this.throwErr();
	},

	/* 抛出异常表单元素及消息 */
	throwErr: function() {
		var len = this.illegalArr.length;
		if(len > 0){
			//清除所有错误信息
			//alert(this.illegalArr[0].getAttribute('msg'));
			//if($("errerBox")) { $("errerBox").parentNode.removeChild($("errerBox")); }
//			var eElements = $$("#"+ this.idForm + " .error");
//			for(var i = eElements.length -1; i > 0; i--){
//				eElements[i].className = eElements[i].className.replace("error","");
//			}
//			//表单元素排序
//			function sortFun(a,b){
//				return (a.tabIndex - b.tabIndex);
//			}
//			
//			this.illegalArr.sort(sortFun);
//			
//			//创建警告信息容器，填充警告信息
//			var div = document.createElement("div");
//			div.id = "errerBox";
//			div.className = $$("table.list")[0].className.replace("list","");
//			
			for(var i = 0; i < len; i++){
				//this.illegalArr[i].style.backgroundColor = "#e42495";
				if(this.illegalArr[i].className.indexOf("error") == -1){
					this.illegalArr[i].className += " error";
				}
				this.illegalArr[i].onclick = clear;
				this.illegalArr[i].onkeyup = clear;
			}
//			
//			$("fld_main").appendChild(div);
			//this.illegalArr[0].focus();
			this.illegalArr = [];
			return false;
		}
		this.preProcess();
		return true;
	}
};

/* 清除错误提示 */
function clear(){
	//this.style.backgroundColor = "#fff";
	this.className = this.className.replace("error","");
}

/* Tab 类 */

function TabClass(){}

TabClass.prototype = {
	init : function(menus,divs,func){
				
		if(menus.length != divs.length){
			alert("菜单层数量和内容层数量不一样!");
			return false;
		}
		
		for(var i = 0 ; i < menus.length ; i++){
			$(menus[i]).index = i;
			$(menus[i]).onclick = function(){
				for(var j = 0 ; j < menus.length ; j++) {
					$(menus[j]).className = "";
					if($(divs[j]).className.indexOf('hide') == -1){
						$(divs[j]).className += " hide";
					}
					
					$$("#"+divs[j]+" input").each(function(node){
						node.disabled = true;
					});
				}
				if(typeof func == 'function'){
					func(this.id);
				}
				$(menus[this.index]).className = "active";
				$(divs[this.index]).className = $(divs[this.index]).className.replace('hide','');
				
				$$("#"+divs[this.index]+" input").each(function(node){
					node.disabled = false;											
				});
				return false;
			}
		}
	}
};
